<template>
    <f7-page>
        <f7-navbar>
            <f7-nav-left>
                <f7-link @click="$f7router.back()">
                    <f7-icon icon="icon-back" style="margin-left:15px;"></f7-icon>
                </f7-link>
            </f7-nav-left>
            <f7-nav-title>{{title}}</f7-nav-title>
        </f7-navbar>
        <f7-block strong 
            style="margin-top:0; margin-bottom:0; padding-top:3px; padding-bottom:3px; font-size:10px;">
            <p style="font-size:14px !important; margin-top:0; margin-bottom:0;">{{subtitle}}</p>
        </f7-block>
        <f7-row style="margin-left:5px; margin-right:10px; margin-top:15px; text-align:center">
            <f7-col>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionContractor" color="green">施工单位</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionSupervisor" color="orange">监理单位</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionConstructor" color="blue">建设单位</f7-badge>
            </f7-col>
        </f7-row>
        <f7-row style="margin-left:5px; margin-right:10px; margin-top:15px; text-align: center">
            <f7-col>
                <f7-badge color="blue">已测层数</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionContractor" color="green">{{measureFloorCountContractor}}</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionSupervisor" color="orange">{{measureFloorCountSupervisor}}</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionConstructor" color="blue">{{measureFloorCountConstructor}}</f7-badge>
            </f7-col>
        </f7-row>
        <f7-row style="margin-left:5px; margin-right:10px; margin-top:15px; text-align: center">
            <f7-col>
                <f7-badge color="blue">总合格率</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionContractor" color="green">{{rateContractor}}%</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionSupervisor" color="orange">{{rateSupervisor}}%</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionConstructor" color="blue">{{rateConstructor}}%</f7-badge>
            </f7-col>
        </f7-row>
        <f7-row inset v-for="floorId in floorIds"
            style="margin-left:5px; margin-right:10px; margin-top:15px; margin-bottom:0; text-align: center;">
            <f7-col>
                {{$store.state.global.floorMap[floorId].floor_name}}F
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionContractor && measureFloorMap[floorId] && measureFloorMap[floorId]['施工单位']" 
                    color="green">{{rateFloor('施工单位', floorId)}}%</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionSupervisor && measureFloorMap[floorId] && measureFloorMap[floorId]['监理单位']" 
                    color="orange">{{rateFloor('监理单位', floorId)}}%</f7-badge>
            </f7-col>
            <f7-col>
                <f7-badge v-show="hasPermissionConstructor && measureFloorMap[floorId] && measureFloorMap[floorId]['建设单位']" 
                    color="blue">{{rateFloor('建设单位', floorId)}}%</f7-badge>
            </f7-col>
        </f7-row>
    </f7-page>
</template>

<script>
import Common from "@/utils/Common.js";
import DateUtil from "@/utils/DateUtil.js";
import $ from 'jquery';
import events from 'events';
import store from '@/store';
import { getUser, setUser, clearUser } from '@/utils/auth';

export default {
    store: store,
    props: {
    	unitId: {
    		type: String
    	},
    	itemId: {
    		type: String
    	}
    },
    data: function() {
        return {
        	floorIds: [],
        	measureFloorMap: {}
        };
    },
    methods: {
    	measureFloorCount: function(type) {
            var count = 0;
            for(var floorId in this.measureFloorMap) {
                var measureFloor = this.measureFloorMap[floorId];
                if(measureFloor[type]) {
                	count++;
                }
            }
            return count;
    	},
    	measurePoints: function(type) {
            var amount = 0;
            for(var floorId in this.measureFloorMap) {
                var measureFloor = this.measureFloorMap[floorId];
                var measure = measureFloor[type];
                if(measure != null) {
                    amount += measure.measure_points;
                }
            }
            return amount;
    	},
        problemPoints: function(type) {
            var amount = 0;
            for(var floorId in this.measureFloorMap) {
                var measureFloor = this.measureFloorMap[floorId];
                var measure = measureFloor[type];
                if(measure != null) {
                    amount += measure.problem_points;
                }
            }
            return amount;
        },
        rateFloor: function(type, floorId) {
        	var measureFloor = this.measureFloorMap[floorId];
        	if(measureFloor == null) {
        		return 0;
        	}
        	var measure = measureFloor[type];
        	if(measure == null) {
        		return 0;
        	} else {
        		var measurePoints = measure.measure_points;
        		var problemPoints = measure.problem_points;
        		var rate = (measurePoints - problemPoints) / measurePoints * 100;
        		rate = rate.toFixed(1);
        		return rate;
        	}
        }
    },
    computed: {
    	title: function() {
    		var unit = this.$store.state.global.unitMap[this.unitId];
    		var building = this.$store.state.global.buildingMap[unit.building_id];
    		return building.building_name + unit.unit_name + '单元'
    	},
    	subtitle: function() {
    		var problemClass = this.$store.state.global.problemClassMap[this.itemId];
    		var parentProblemClass = this.$store.state.global.problemClassMap[problemClass.parent_problem_class_id];
    		return parentProblemClass.problem_class_name + '-' + problemClass.problem_class_name;
    	},
        hasPermissionContractor: function() {
            var userId = getUser().userId;
            var user = this.$store.state.global.userMap[userId];
            if(user.app_permission == '施工单位' || user.app_permission == '监理单位' || user.app_permission == '建设单位') {
                return true;
            } else {
                return false;
            }
        },
        hasPermissionSupervisor: function() {
            var userId = getUser().userId;
            var user = this.$store.state.global.userMap[userId];
            if(user.app_permission == '监理单位' || user.app_permission == '建设单位') {
                return true;
            } else {
                return false;
            }
        },
        hasPermissionConstructor: function() {
            var userId = getUser().userId;
            var user = this.$store.state.global.userMap[userId];
            if(user.app_permission == '建设单位') {
                return true;
            } else {
                return false;
            }
        },
        measureFloorCountContractor: function() {
        	return this.measureFloorCount('施工单位');
        },
        measureFloorCountSupervisor: function() {
        	return this.measureFloorCount('监理单位');
        },
        measureFloorCountConstructor: function() {
        	return this.measureFloorCount('建设单位');
        },
        rateContractor: function() {
        	var measurePoints = this.measurePoints('施工单位');
        	var problemPoints = this.problemPoints('施工单位');
        	if(measurePoints == 0) {
        		return 0;
        	} else {
        		var rate = (measurePoints - problemPoints) / measurePoints * 100;
        		rate = rate.toFixed(1);
        		return rate;
        	}
        },
        rateSupervisor: function() {
            var measurePoints = this.measurePoints('监理单位');
            var problemPoints = this.problemPoints('监理单位');
            if(measurePoints == 0) {
                return 0;
            } else {
                var rate = (measurePoints - problemPoints) / measurePoints * 100;
                rate = rate.toFixed(1);
                return rate;
            }
        },
        rateConstructor: function() {
            var measurePoints = this.measurePoints('建设单位');
            var problemPoints = this.problemPoints('建设单位');
            if(measurePoints == 0) {
                return 0;
            } else {
                var rate = (measurePoints - problemPoints) / measurePoints * 100;
                rate = rate.toFixed(1);
                return rate;
            }
        }
    },
    watch: {
        
    },
    created: function() {
    	var self = this;
    	var unit = this.$store.state.global.unitMap[this.unitId];
    	var floors = [];
    	for(var floorId in this.$store.state.global.floorMap) {
    		var floor = this.$store.state.global.floorMap[floorId];
    		if(floor.building_id == unit.building_id) {
    			floors.push(floor);
    		}
    	}
    	floors.sort(function(floor1, floor2) {
            var f1 = parseInt(floor1.floor_name);
            if(isNaN(f1)) {
                return 1;
            }
            var f2 = parseInt(floor2.floor_name);
            if(isNaN(f2)) {
                return -1;
            }
            if(f1 < f2) {
                return -1;
            } else if(f1 == f2) {
                return 0
            } else {
                return 1;
            }
        });
    	this.floorIds = floors.map(function(floor) {
    		return floor.id;
    	});
    	let sql = "\
		    select t.floor_id, t.app_permission, count(*) as measure_count, \
		        sum(measure_points) as measure_points, sum(problem_points) as problem_points \
		    from tb_quality_measure_checkquality_problem t \
		    where t.unit_id = '" + self.unitId + "' \
		    and t.item_id = '" + self.itemId + "' \
		    group by t.floor_id, t.app_permission \
		";
    	Common.executeSql(sql, []).then((result) => {
    		let rs = result.rs;
    		for(var i = 0; i < rs.rows.length; i++) {
				var row = rs.rows.item(i);
				var measureFloor = self.measureFloorMap[row.floor_id];
				if(measureFloor == null) {
					measureFloor = {};
					self.$set(self.measureFloorMap, row.floor_id, measureFloor);
				}
                self.$set(measureFloor, row.app_permission, row);
			}
    	}).catch(error => {
			console.error(error);
		});
    	
    },
    mounted: function() {
    	
    }
}
</script>

<style scoped>

</style>